package com.moon.game.service;

import org.apache.log4j.Logger;

import com.google.protobuf.ByteString;
import com.moon.game.database.MongoUtil;
import com.moon.game.util.ProfileConfig;

public class ProfileService {
	private static final String TAG_PLAYER = "player";
	private static final Logger log = Logger.getLogger(ProfileService.class);
	/**
	 * 保存人物到mongo
	 * 
	 * @param playerId
	 * @param data
	 */
	public static void savePlayer2DB(String playerId,ByteString data) {
		int index = (int)(Math.abs((playerId+"1").hashCode()) % ProfileConfig.getProfileNum());
		MongoUtil.saveData2Db(ProfileConfig.getProfile(index), playerId, TAG_PLAYER, TAG_PLAYER, data.toByteArray());
	}
	
	
	/**
	 * 从mongo中获取人物信息
	 * 
	 * @param playerId
	 * @return
	 */
	public static ByteString getPlayerFromDB(String playerId) {
		int index = (int)(Math.abs((playerId+"1").hashCode()) % ProfileConfig.getProfileNum());
		try {
			ByteString data = MongoUtil.getDataFromDb(ProfileConfig.getProfile(index), playerId, TAG_PLAYER, TAG_PLAYER);
			return data;
		} catch (Exception e) {
			log.warn("获取人物异常！！" + e.getMessage(),e);
			return null;
		}
	}
}
